package top.hkyzf.study.leetcode.tree;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-20 21:26
 */
public class 对称二叉树_101 {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        return check(left, right);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        if (left.val == right.val) {
            return check(left.left, right.right) && check(left.right, right.left);
        } else {
            return false;
        }
    }

    @Test
    public void testIsSymmetric () {
        TreeNode node6 = new TreeNode(3);
        TreeNode node5 = new TreeNode(4);
        TreeNode node4 = new TreeNode(4);
        TreeNode node3 = new TreeNode(3);
        TreeNode node2 = new TreeNode(2, node5, node6);
        TreeNode node1 = new TreeNode(2, node3, node4);
        TreeNode root = new TreeNode(1, node1, node2);
        System.out.println(isSymmetric(root));
    }
}
